package com.his_voice.voice.server;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MyWebSocketClient extends WebSocketClient {

    private String excptMessage;

    public MyWebSocketClient(URI serverUri, Draft_6455 draft6455) {
        super(serverUri);
    }
    private static final Logger logger = LoggerFactory.getLogger(MyWebSocketClient.class);

    /**
     * 连接后触发的方法
     * @param serverHandshake
     */
    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        logger.info("开启了连接");
    }

    /**
     * 消息转发
     * @param s
     */
    @Override
    public void onMessage(String s) {
        this.excptMessage = s;
        logger.info("发送送了消息 "+s+"");
    }

    /**
     * 关闭链接
     * @param i
     * @param s
     * @param b
     */
    @Override
    public void onClose(int i, String s, boolean b) {
        logger.info("关闭闭了链接 "+newDate()+"");
    }

    /**
     * 生成一个日期
     * @return
     */
    private String newDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(new Date());
    }

    /**
     * 发生错误
     * @param e
     */
    @Override
    public void onError(Exception e) {
        logger.info("连接发生错误！");
    }

    /**
     * 返回消息
     * @return
     */
    public String getExcptMessage () {
        if (excptMessage != null) {
            String message = getContent(excptMessage);
            excptMessage = null;
            return message;
        }
        return null;
    }

    // 把转写结果解析为句子
    public static String getContent(String message) {
        StringBuffer resultBuilder = new StringBuffer();
        try {
            JSONObject messageObj = JSON.parseObject(message);
            JSONObject cn = messageObj.getJSONObject("cn");
            JSONObject st = cn.getJSONObject("st");
            JSONArray rtArr = st.getJSONArray("rt");
            for (int i = 0; i < rtArr.size(); i++) {
                JSONObject rtArrObj = rtArr.getJSONObject(i);
                JSONArray wsArr = rtArrObj.getJSONArray("ws");
                for (int j = 0; j < wsArr.size(); j++) {
                    JSONObject wsArrObj = wsArr.getJSONObject(j);
                    JSONArray cwArr = wsArrObj.getJSONArray("cw");
                    for (int k = 0; k < cwArr.size(); k++) {
                        JSONObject cwArrObj = cwArr.getJSONObject(k);
                        String wStr = cwArrObj.getString("w");
                        resultBuilder.append(wStr);
                    }
                }
            }
        } catch (Exception e) {
            return message;
        }

        return resultBuilder.toString();
    }
}
